package com.kizuki.springcloud.controller;

import com.kizuki.springcloud.pojo.Dept;
import com.kizuki.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/dept/get/{id}")
    public Dept queryDeptById(@PathVariable("id") int id){
        return Objects.requireNonNull(deptService.queryDeptById(id));
    }

    public Dept hystrixGet(int id) {
        return new Dept()
                .setDeptno(id)
                .setDeptname("cant find this dept")
                .setDb_source("no db in mysql");
    }

    @PostMapping("/dept/add")
    public boolean addDept(@RequestBody Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/list")
    public List<Dept> queryDepts() {
        return deptService.queryDepts();
    }

}
